﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HS.EIP.WatchService
{
    internal static class HttpHealthCheck
    {
        private static HttpClient HttpClient = new HttpClient();
        static HttpHealthCheck()
        {
            HttpClient.Timeout = TimeSpan.FromSeconds(20);
        }

        internal static async Task<bool> Health(Service service)
        {
            if (string.IsNullOrWhiteSpace(service.Health))
                throw new Exception($"类型为{service.Health}的服务必须配置健康检查路径。");
            try
            {
                using (var response = await HttpClient.GetAsync(service.Health))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
